#include "swpendpointgraphicitem.h"

#include "graphicitemcommon.h"

using namespace core;
using namespace gui;
using namespace swp;

SwpEndPointGraphicItem::Content::Content(QGraphicsItem* parent) :
    EllipseGraphicItem(true, false, parent), IGraphicItemContent()
{
    QRectF rect(0.5, 0.5, 40.5, 40.5);

    setRect(rect);

    setPen(QPen(Qt::transparent, 0));
    setBrush(QBrush(Qt::black));
}

SwpEndPointGraphicItem::Content::~Content()
{

}

// IGraphicItemContent
QGraphicsItem* SwpEndPointGraphicItem::Content::graphicsItem() const
{
    return const_cast<SwpEndPointGraphicItem::Content* >(this);
}

void SwpEndPointGraphicItem::Content::drawThumbnail(QPainter& painter,
    const QRectF& boundingRect)
{
    const qreal coefficient =  qMin(
        boundingRect.width() /
        this->boundingRect().width(),
        boundingRect.height() /
        this->boundingRect().height());

    const qreal wDiff = 10;
    const qreal hDiff = 10;

    QTransform transform;
    transform.scale(coefficient, coefficient);
    QRectF transformed = transform.mapRect(this->boundingRect());
    transformed.setWidth(transformed.width() - wDiff);
    transformed.setHeight(transformed.height() - hDiff);
    transformed.setX(0);
    transformed.setY(0);

    QRectF _transformed = transformed.translated(
        (boundingRect.width() + (boundingRect.x() * 2) - transformed.width())
            / 2,
        (boundingRect.height() + (boundingRect.y() * 2) -
            transformed.height()) / 2);

    painter.setPen(pen());
    painter.setBrush(brush());

    painter.drawEllipse(_transformed);
}

QRectF SwpEndPointGraphicItem::Content::croppedBoundingRect() const
{
    const QRectF ret = EllipseGraphicItem::boundingRect();

    return ret;
}

void SwpEndPointGraphicItem::Content::recheckIfFits()
{
    // do nothing
}

void SwpEndPointGraphicItem::Content::setPalette(const Palette* const palette)
{

}

// ISerializable
bool SwpEndPointGraphicItem::Content::doActionForProperty(
    const QString& property, const QVariantList& value,
    const SerializationEngine& helper)
{
    return true;
}

void SwpEndPointGraphicItem::Content::deserializationFinished(bool ok)
{

}

QHash<QString, QVariantList>
    SwpEndPointGraphicItem::Content::serializationData(
        const SerializationEngine& helper) const
{
    QHash<QString, QVariantList> ret;

    return ret;
}

SwpEndPointGraphicItem::SwpEndPointGraphicItem(QGraphicsItem* parent) :
    AbstractEllipseGraphicItem(parent)
{
    QRectF rect(0.5, 0.5, 50.5, 50.5);

    setRectF(rect);

    setFlags(flags() | QGraphicsItem::ItemIsMovable |
        QGraphicsItem::ItemIsSelectable);

    setContent(new Content(this));
}

SwpEndPointGraphicItem::~SwpEndPointGraphicItem()
{

}

int SwpEndPointGraphicItem::paletteType() const
{
    return Background | Border;
}

bool SwpEndPointGraphicItem::supportsConnection(
    const core::ConnectionData& connection) const
{
    bool ret = false;

    const int type = connection.From != 0 ? connection.From->type() :
        INVALID_TYPE;

    ret = (type == SWP_START_POINT_ITEM || type == SWP_ACTIVITY_ITEM ||
        type == SWP_DECISION_ITEM || type == SWP_JOIN_ITEM ||
        type == SWP_MERGE_ITEM) &&
            connection.ConnectionId == CONNECTION_ACTIVITY;

    return ret;
}

int SwpEndPointGraphicItem::type() const
{
    return SWP_END_POINT_ITEM;
}

QString SwpEndPointGraphicItem::name() const
{
    return QObject::tr("End point");
}

IEditorItem* SwpEndPointGraphicItem::copy() const
{
    return new SwpEndPointGraphicItem(0);
}

QList<core::Property> SwpEndPointGraphicItem::properties() const
{
    QList<Property> ret;

    return ret;
}

QVariant SwpEndPointGraphicItem::propertyValue(
        const QString& propertyName) const
{
    QVariant ret;

    return ret;
}

void SwpEndPointGraphicItem::propertyChanged(const QString& property,
    const QVariant& newValue, bool silent)
{
    // do nothing
}

QVariantList SwpEndPointGraphicItem::optionList(
    const QString& propertyName) const
{
    QVariantList ret;

    return ret;
}

QVariantList SwpEndPointGraphicItem::autocomplete(
    const QString& propertyName) const
{
    QVariantList ret;

    return ret;
}

QList<QPointF> SwpEndPointGraphicItem::scalePoints() const
{
    QList<QPointF> ret;

    ret << QPointF(0.0, 0.0) << QPointF(50, 0)
        << QPointF(50, 50) << QPointF(0, 50);

    return ret;
}

QList<QPointF> SwpEndPointGraphicItem::connectPoints() const
{
    QList<QPointF> ret;

    ret << QPointF(25, 0.0) << QPointF(50, 25)
        << QPointF(25, 50) << QPointF(0, 25);

    return ret;
}

QList<QPointF> SwpEndPointGraphicItem::selfConnectAnchorPoints() const
{
    QList<QPointF> ret;

    ret << QPointF(25, -20.0) << QPointF(70, 25)
        << QPointF(25, 70) << QPointF(-20, 25);

    return ret;
}
